package com.infoauto.opc.gateway.messages.configuration;

import com.infoauto.opc.gateway.messages.factory.RabbitMQFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// RabbitMQ配置 AMQP协议
@Configuration
@ConditionalOnExpression("'${spring.rabbitmq.addresses:}'!=''")
public class RabbitMQConfiguration {

    @Value("${spring.rabbitmq.addresses:}")
    private String addresses;

    @Value("${spring.rabbitmq.username:}")
    private String username;

    @Value("${spring.rabbitmq.password:}")
    private String password;

    @Value("${spring.rabbitmq.virtualHost:/}")
    private String vhost;

    @Bean
    public CachingConnectionFactory cachingConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(addresses);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        return connectionFactory;
    }

    @Bean
    public RabbitMQFactory rabbitMQFactory(CachingConnectionFactory cachingConnectionFactory) {
        return new RabbitMQFactory(cachingConnectionFactory);
    }
}
